package mylibrary.httpclient;

import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class PostSample {

	public static void main(String[] args) throws HttpException, IOException {
		
		HttpClient httpClient = new HttpClient();
		
		PostMethod postMethod = new PostMethod("http://www.renren.com/PLogin.do");
		
		NameValuePair[] data = {new NameValuePair("email", "gaoshuaidage@163.com"), new NameValuePair("password", "gaoshuai2003"),
				new NameValuePair("submit", "登录")};
		postMethod.setRequestBody(data);
		int statusCode = httpClient.executeMethod(postMethod);			//302
		Header locationHeader = postMethod.getResponseHeader("location");
		
		postMethod = new PostMethod(locationHeader.getValue());
		statusCode = httpClient.executeMethod(postMethod);				//302	
		locationHeader = postMethod.getResponseHeader("location");
		System.out.println(locationHeader.getValue());
		
		GetMethod getMethod = new GetMethod(locationHeader.getValue());
		statusCode = httpClient.executeMethod(getMethod);
		System.out.println(statusCode);
		System.out.println(getMethod.getResponseBodyAsString());
		
//		InputStream is = getMethod.getResponseBodyAsStream();
//		BufferedReader in = new BufferedReader(new InputStreamReader(is)); 
//	    StringBuffer buffer = new StringBuffer(); 
//	    String line = ""; 
//	    while ((line = in.readLine()) != null){ 
//	      buffer.append(line); 
//	    } 
//	    System.out.println(buffer.toString());   

	}
}
